<?php
/*
Plugin Name: Field Layout Manager
Plugin URI: http://www.encyclomoto.com/
Description: Field Layout Manager lets you display your custom fields in a sorted html table. FML also lets you group fields and give them custom names. 
Author: Zack Preble
Version: 0.0.1
Author URI: http://www.encyclomoto.com/

Copyright (c) 2007 Zack Preble
Search Custom Fields is released under the GNU General Public License
(GPL) http://www.gnu.org/licenses/gpl.txt

This is a WordPress 2 plugin (http://wordpress.org).
*/

  function flm_get_custom_fields() {
    $file = dirname( __FILE__ ) . '/conf.ini';
    if ( !file_exists( $file ) )
      return null;  

    $custom_fields = parse_ini_file( $file, true );
    return $custom_fields;
  }

  function flm_get_config_file()
  {
    $file = dirname( __FILE__ ) . '/conf.ini';
	 if ( !file_exists( $file ) )
      return null;
    $fh = fopen($file, 'r') or die("Can't open file");
	 $contents = fread($fh, filesize($file));;
    fclose($fh); 
	 return $contents; 
  }
  
  function flm_set_config_file($content)
  {
    $file = dirname( __FILE__ ) . '/conf.ini';
	 if ( !file_exists( $file ) )
      return null;
  }
  
  
  function flm_get_custom_field_datagrid()
  {
    $fields = flm_get_custom_fields();
    echo "<table class=\"customfielddatagrid\">\n";
    foreach( $fields as $title => $data ) {
      echo "\t<tr><th colspan=\"2\">".$title."</th></tr>\n";
      foreach($data as $key => $fieldName)
      {      
        $itemValues = get_post_custom_values($key);
        if($itemValues != NULL )
        {  
          if( $fieldName != NULL )
          {
            echo "\t<tr><td class=\"customfieldkey\">".$fieldName."</td>";
          }  
          else
          {
            echo "\t<tr><td class=\"customfieldkey\">".$key."</td>";
          }
          echo "<td class=\"customfieldvalue\">".implode(", ",$itemValues)."</td></tr>\n"; 
        }
      }
    }
    echo "</table>";
  }
  
  
  
  function flm_config()
  {
  if ( $_POST['flm_config'] ){
    $content = $_POST['flm_config'] ;
    //flm_set_config_file($content);
  }
  
   $text = flm_get_config_file();
	print '
	<div class="wrap">
		<h2>Field Layout Manager</h2>
		<p>Your current config file:</p>
		<textarea id="flm_config" name="flm_config" rows="20" cols="30" class="widefat">'.$text.'</textarea>
		<p>Edit the conf.ini file located in the plugin directory to modify layour.</p>
	</div>
	';
  }
  
  function flm_admin_page()
  {
		add_options_page('Field Layout Manager', 'Field Layout Manager', 8, basename(__FILE__), 'flm_config');
  }

add_action('admin_menu', 'flm_admin_page');

?>